package com.sample.ab;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @author 张智研
 * @date 2024/01/16 10:31
 **/
public class Sha256 {

    public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
        String input = "S3&2e3d4cbd1e96497c&" + System.currentTimeMillis(); // 输入需要加密的字符串
        System.out.println(input);
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hashBytes = digest.digest(input.getBytes());

        StringBuilder sb = new StringBuilder();
        for (byte b : hashBytes) {
            sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        }

        System.out.println("SHA-256加密结果为：" + sb.toString());

        // base64 发票
        String filePath = "/Users/zhangzhiyan/Desktop/workspace/testfapiao.pdf";
        File file = new File(filePath);
        byte[] dataBytes = Files.readAllBytes(file.toPath());
        String base64String = Base64.getEncoder().encodeToString(dataBytes);
        System.out.println("base64String: "+base64String);
    }
}
